﻿namespace KinonekoSoftware.Foundation.UX.Controls
{
    public sealed class ChatBubble : ContentControl
    {
        public static readonly StyledProperty<string>           ActorNameProperty;
        public static readonly StyledProperty<string>           ActorTitleProperty;
        public static readonly StyledProperty<bool>             IsSelfProperty;
        public static readonly StyledProperty<IBrush>           ActorGravatarProperty;
        public static readonly StyledProperty<IBrush>           ActorTitleBrushProperty;
        public static readonly DirectProperty<ChatBubble, bool> IsTitleEmptyProperty;

        static ChatBubble()
        {
            ActorNameProperty       = AvaloniaProperty.Register<ChatBubble, string>(nameof(ActorName));
            ActorTitleProperty      = AvaloniaProperty.Register<ChatBubble, string>(nameof(ActorTitle));
            IsSelfProperty          = AvaloniaProperty.Register<ChatBubble, bool>(nameof(IsSelf));
            ActorGravatarProperty   = AvaloniaProperty.Register<ChatBubble, IBrush>(nameof(ActorGravatar));
            ActorTitleBrushProperty = AvaloniaProperty.Register<ChatBubble, IBrush>(nameof(ActorTitleBrush));
            IsTitleEmptyProperty    = AvaloniaProperty.RegisterDirect<ChatBubble, bool>(nameof(IsTitleEmpty), x => x._isTitleEmpty);

            ActorTitleProperty.Changed.Subscribe(x =>
            {
                var sender = (ChatBubble)x.Sender;
                sender.IsTitleEmpty = string.IsNullOrEmpty(x.GetNewValue<string>());
            });
        }

        private bool _isTitleEmpty;

        public bool IsTitleEmpty
        {
            get => _isTitleEmpty;
            private set => SetAndRaise(IsTitleEmptyProperty, ref _isTitleEmpty, value);
        }

        public IBrush ActorTitleBrush
        {
            get => GetValue(ActorTitleBrushProperty);
            set => SetValue(ActorTitleBrushProperty, value);
        }


        public bool IsSelf
        {
            get => GetValue(IsSelfProperty);
            set => SetValue(IsSelfProperty, Boxing.Box(value));
        }

        public string ActorTitle
        {
            get => GetValue(ActorTitleProperty);
            set => SetValue(ActorTitleProperty, value);
        }

        public IBrush ActorGravatar
        {
            get => GetValue(ActorGravatarProperty);
            set => SetValue(ActorGravatarProperty, value);
        }

        public string ActorName
        {
            get => GetValue(ActorNameProperty);
            set => SetValue(ActorNameProperty, value);
        }
    }
}